package com.liang.rabbitmqspringboot.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    private  RabbitTemplate rabbitTemplate;

    public void makeOrder(String userid, String productid, int num){
        // 1: 根据商品id查询库存是否充足
        // 2: 保存订单
        String orderid = UUID.randomUUID().toString();
        System.out.println("订单生成成功"+orderid);
        //// 3: 通过Mp来完成消息的分发
        //参数1: 交换机 参数2:路中key/queue队列名称 参数3:消息内容
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderid);

    }

    public void makeOrderDirect(String userid, String productid, int num){
        // 1: 根据商品id查询库存是否充足
        // 2: 保存订单
        String orderid = UUID.randomUUID().toString();
        System.out.println("订单生成成功"+orderid);
        //// 3: 通过Mp来完成消息的分发
        //参数1: 交换机 参数2:路中key/queue队列名称 参数3:消息内容
        String exchangeName = "direct_order_exchange";
        rabbitTemplate.convertAndSend(exchangeName,"email",orderid);
        rabbitTemplate.convertAndSend(exchangeName,"duanxin",orderid);

    }

    public void makeOrderTopic(String userid, String productid, int num){
        // 1: 根据商品id查询库存是否充足
        // 2: 保存订单
        String orderid = UUID.randomUUID().toString();
        System.out.println("订单生成成功"+orderid);
        //// 3: 通过Mp来完成消息的分发
        //参数1: 交换机 参数2:路中key/queue队列名称 参数3:消息内容
        String exchangeName = "topic_order_exchange";
        String routKey  = "duanxin";
        //#.duanxin#
        //#.email#
        //#com.#
        rabbitTemplate.convertAndSend(exchangeName,routKey,orderid);

    }

    public void makeOrderTTL(String userid, String productid, int num){
        // 1: 根据商品id查询库存是否充足
        // 2: 保存订单
        String orderid = UUID.randomUUID().toString();
        System.out.println("订单生成成功"+orderid);
        //// 3: 通过Mp来完成消息的分发
        //参数1: 交换机 参数2:路中key/queue队列名称 参数3:消息内容
        String exchangeName = "ttl_direct_exchange";
        String routKey  = "ttl";
        rabbitTemplate.convertAndSend(exchangeName,routKey,orderid);

    }

    public void makeOrderTTLMessage(String userid, String productid, int num){
        // 1: 根据商品id查询库存是否充足
        // 2: 保存订单
        String orderid = UUID.randomUUID().toString();
        System.out.println("订单生成成功"+orderid);
        //// 3: 通过Mp来完成消息的分发
        //参数1: 交换机 参数2:路中key/queue队列名称 参数3:消息内容
        String exchangeName = "ttl_direct_exchange";
        String routKey  = "ttlMessage";

        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
              message.getMessageProperties().setExpiration("5000");
              message.getMessageProperties().setContentEncoding("utf-8");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName,routKey,orderid,messagePostProcessor);
    }
}
